package guoan.service;

import guoan.domain.CoachItem;
import guoan.domain.CoachItemCheck;
import guoan.domain.CoachItemCheckPK;
import guoan.repository.CoachItemCheckRepository;

import java.util.List;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false)
public class CoachItemCheckService extends PageSerivce<CoachItem> {
	protected final Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	private CoachItemCheckRepository coachItemCheckRepository;

	public void deleteByCoachIdAndOpenHours(Long coachId, String moEq) {
		// TODO Auto-generated method stub
		List<CoachItemCheck> coachItemCheck = coachItemCheckRepository.findByCoachIdAndOpenHours(coachId, moEq);
		coachItemCheckRepository.delete(coachItemCheck);
	}

	public void deleteByCoachId(Long coachId) {
		// TODO Auto-generated method stub
		List<CoachItemCheck> coachItemCheck = coachItemCheckRepository.findByCoachId(coachId);
		coachItemCheckRepository.delete(coachItemCheck);
	}

	public void deleteOne(CoachItemCheckPK coachItemCheckPK) {
		// TODO Auto-generated method stub
		CoachItemCheck coachItemCheck = coachItemCheckRepository.findOne(coachItemCheckPK);
		if (coachItemCheck != null) {
			coachItemCheckRepository.delete(coachItemCheck);
		}
	}

	@Transactional(readOnly = false, rollbackFor = DataIntegrityViolationException.class, propagation = Propagation.REQUIRES_NEW)
	public Boolean checkSave(String itemHours, Long coachId, DateTime coachTime, Long trainId) {
		// TODO Auto-generated method stub
		coachItemCheckRepository.insert(coachId, itemHours, coachTime.toString("yyyy-MM-dd"), trainId);
		return true;
	}

}
